package activities;

import java.util.ArrayList;

import bookless.pakage.Book;
import bookless.pakage.R;
import bookless.pakage.UserHandler;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MyPageActivity extends Activity{

	private static Context context;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		context = MyPageActivity.this;
		super.onCreate(savedInstanceState);
		setContentView(R.layout.mypage);
		
		Button addBook;
		addBook = (Button) findViewById(R.id.addBook);
		addBook.setOnClickListener(new View.OnClickListener() {
	        
        @Override
        public void onClick(View v) {
        		startActivity(new Intent("bookless.pakage.ADDBOOK"));	
        	}
        });
		
		Button removeBook;
		removeBook = (Button) findViewById(R.id.removeBook);
		removeBook.setOnClickListener(new View.OnClickListener() {
	        
        @Override
        public void onClick(View v) {
        		startActivity(new Intent("activities.REMOVEBOOK"));	
        	}
        });
		
		Button myList;
		myList = (Button) findViewById(R.id.myBookList);
		myList.setOnClickListener(new View.OnClickListener() {
		
    	@Override
    	public void onClick(View v) {
    		
    		ArrayList<Book> books = UserHandler.getMyBooks();
    		    		
    		printBooks(books);
    	}
		});
		
		Button search;
		search = (Button) findViewById(R.id.loginSearch);
		search.setOnClickListener(new View.OnClickListener() {
		
    	@Override
    	public void onClick(View v) {
    		
    		startActivity(new Intent("bookless.pakage.SEARCH"));
    		
    	}
		});
		
		Button logout;
		logout = (Button) findViewById(R.id.logout);
		logout.setOnClickListener(new View.OnClickListener() {
		
    	@Override
    	public void onClick(View v) {
    		UserHandler.session = null;
    		startActivity(new Intent("bookless.pakage.LOGIN"));
    	}
		});		
		
        Button removeUser;
        removeUser = (Button)findViewById(R.id.removeUser);
        removeUser.setOnClickListener(new View.OnClickListener() {
        
        	@Override
        	public void onClick(View v) {
        		
        	UserHandler.removeUser();
        	
    		UserHandler.session = null;
    		Dialog d = new Dialog(MyPageActivity.this);
			d.setTitle("Account Removed");
			TextView tv = new TextView(MyPageActivity.this);
			d.setContentView(tv);
			d.show();
			
			startActivity(new Intent("activities.REGISTER"));

        	}
        });
        
    }
	
	public void printBooks(ArrayList<Book> booksList) {


		int length = booksList.size();		
		LinearLayout listLayout = new LinearLayout(this);
		setContentView(listLayout);
		listLayout.setOrientation(LinearLayout.VERTICAL);

		TextView tv = null;
		if(length != 0){
			for (int i = 0 ; i < length;i++ ) {
				String text = booksList.get(i).toString();
				tv = new TextView(getApplicationContext());
				tv.setText(text);
				listLayout.addView(tv);
			}
		}else{
			tv = new TextView(getApplicationContext());
			tv.setText("You have no book adds");
			listLayout.addView(tv);
		}
		
		Button back = new Button(getApplicationContext());
		back.setWidth(6);
		back.setText("Go back to My Page");
		listLayout.addView(back);

		back.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {

				startActivity(new Intent("bookless.pakage.MYPAGE"));
			}
		});
	}
	
	public static Context getContext() {
		return context;
	}


}

	
	

